package com.weibo4j;

import java.io.File;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import weiboclient4j.FriendshipService;
import weiboclient4j.StatusService;
import weiboclient4j.WeiboClientException;
import weiboclient4j.model.Status;
import weiboclient4j.model.User;
import weiboclient4j.model.UserList;
import weiboclient4j.oauth2.DisplayType;
import weiboclient4j.oauth2.GrantType;
import weiboclient4j.oauth2.ResponseType;
import weiboclient4j.oauth2.SinaWeibo2AccessToken;
import weiboclient4j.params.Paging;
import weiboclient4j.params.StatusParam;
import weiboclient4j.params.Uid;

@Component
public class WeiboSpi extends WeiboService {

	@Value("#{properties['weibo.callback.uri']}")
	private String callbackUri;

	@Value("#{properties['weibo.state']}")
	private String state;

	private static final Logger logger = LoggerFactory
			.getLogger(WeiboSpi.class);

	public String getRedirectUrl() {
		try {
			String url = this.getWeiboClient().getAuthorizationUrl(
					ResponseType.Code, DisplayType.Default, state,
					this.callbackUri);
			return url;
		} catch (Exception ex) {
			logger.error("Failed to get request token url.", ex);
		}
		return null;
	}

	public SinaWeibo2AccessToken getAccessToken(String code) {
		try {
			System.out
					.println("WeiboAuthorizationServiceAdapter getAccessToken");
			SinaWeibo2AccessToken accessToken = this.getWeiboClient()
					.getAccessToken(GrantType.AuthorizationCode, code,
							this.callbackUri);
			System.out
					.println(" WeiboAuthorizationServiceAdapter getAccessToken uid"
							+ accessToken.getUid());
			return accessToken;
		} catch (Exception ex) {
			logger.error("Failed to get request token url.", ex);
			return null;
		}
	}

	public UserList getFriendList(String uid, String accessTokenStr) {
		System.out.println("WeiboAuthorizationServiceAdapter getFriendList "
				+ accessTokenStr);
		this.getWeiboClient().setAccessToken(
				new SinaWeibo2AccessToken(accessTokenStr));
		FriendshipService fs = this.getWeiboClient().getFriendshipService();
		
		Paging page = Paging.create();
		
		page = page.count(5);
		page = page.page(1);
		
		try {
			return fs.getFriends(new Uid(Long.parseLong(uid)), page);
		} catch (WeiboClientException ex) {
			logger.error("Failed to get request token url.", ex);
		}
		return null;
	}

	public User getUser(String accessToken, String weiboUid) {

		try {
			return this.getWeiboClient().getUserService()
					.show(new Uid(Long.parseLong(weiboUid)));
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return null;
		} catch (WeiboClientException e) {
			e.printStackTrace();
			return null;
		}
	}

	public Status publishWeibo(String accessTokenCode) {
		try {

			this.getWeiboClient().setAccessToken(
					new SinaWeibo2AccessToken(accessTokenCode));

			StatusService statusService = this.getWeiboClient()
					.getStatusService();

			Status uploadedStatusByImageUrl = statusService.uploadImageBinary(
					new StatusParam("test post image"), new File(
							"F:\\upload.jpg"));
			return uploadedStatusByImageUrl;
		} catch (WeiboClientException e) {
			e.printStackTrace();
			return null;
		}
	}
}
